package com.brillilab.starter.controller.aio.print;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.common.KeyValue;
import com.brillilab.domain.vo.print.BoxPrintVo;
import com.brillilab.domain.vo.print.DevicePrintVo;
import com.brillilab.domain.vo.print.PanelPrintVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.space.BoxLogic;
import com.brillilab.service.logic.space.DeviceLogic;
import com.brillilab.service.logic.space.PanelLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: PrintController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/1/3111:24
 */
@RestController
@RequestMapping("/api/print")
public class PrintController {
    @Resource
    private DeviceLogic deviceLogic;
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private PanelLogic panelLogic;
    @Resource
    private BoxLogic boxLogic;

    @GetMapping("/device")
    public ResponseVo getDeviceList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
        UserInfoVo user = usersLogic.getUserByToken(token);
        List<KeyValue<String, List<DevicePrintVo>>> printList = deviceLogic.getPrintList(user.getLabId());
        return ResponseVo.success(printList);
    }

    @GetMapping("/panel")
    public ResponseVo getPanelList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
        UserInfoVo user = usersLogic.getUserByToken(token);
        List<KeyValue<String, List<KeyValue<String, List<PanelPrintVo>>>>> printList = panelLogic.getPrintList(user.getLabId());
        return ResponseVo.success(printList);
    }

    @GetMapping("/box")
    public ResponseVo getBoxList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
        UserInfoVo user = usersLogic.getUserByToken(token);
        List<KeyValue<String, List<KeyValue<String, List<BoxPrintVo>>>>> printList = boxLogic.getPrintList(user.getLabId());
        return ResponseVo.success(printList);
    }

    @GetMapping("/print")
    public ResponseVo print(Long deviceId, Long panelId, Long boxId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.isTrue(deviceId != null || panelId != null || boxId != null, "参数缺失");
        UserInfoVo user = usersLogic.getUserByToken(token);
        if (deviceId != null) {
            return ResponseVo.success(deviceLogic.getPrint(user, deviceId));
        } else if (panelId != null) {
            return ResponseVo.success(panelLogic.getPrint(user, panelId));
        } else {
            return ResponseVo.success(boxLogic.getPrint(user, boxId));
        }
    }
}
